package com.liu.case1;

import java.io.File;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/8/6 19:55
 * @Description
 */
public class FileCountCase {

    // TODO 使用java中的File类来统计某个目录的大小，和这个目录下面文件的个数和文件夹的个数。在控制台输出。

    private Long size;
    private String path;
    private File file;

    private Integer countFile = 0;
    private Integer countFolder = -1;

    public FileCountCase(String path) {

        this.path = path;
        this.file = new File(path);
        sum(this.file);
    }

    private void sum(File file_) {

        if (file_.exists() && null != file_)
            if (file_.isFile()) {
                size = size + file_.length();
                countFile ++;
            } else  {
                countFolder ++;
                for (File f1 : file_.listFiles())
                    sum(f1);
            }

    }

    public static void main(String[] args) {

        FileCountCase f1 = new FileCountCase("D:/day1");
        System.out.println(f1.getSize());

    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Integer getCountFile() {
        return countFile;
    }

    public void setCountFile(Integer countFile) {
        this.countFile = countFile;
    }

    public Integer getCountFolder() {
        return countFolder;
    }

    public void setCountFolder(Integer countFolder) {
        this.countFolder = countFolder;
    }
}
